<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryForm" :model="queryParams" size="small" :inline="true" label-width="68px">
      <el-form-item label="进仓验货单编号" prop="id">
        <el-input
          v-model="queryParams.id"
          placeholder="请输入进仓验货单编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="采购单ID" prop="purchaseId">
        <el-input
          v-model="queryParams.purchaseId"
          placeholder="请输入采购单ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建日期" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime"
          clearable
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择创建日期"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['erp:put_house:edit']"
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['erp:put_house:remove']"
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['erp:put_house:export']"
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出</el-button>
      </el-col>
      <right-toolbar :show-search.sync="showSearch" @queryTable="getList" />
    </el-row>

    <el-table v-loading="loading" :data="put_houseList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="进仓验货单编号" align="center" prop="id" />
      <el-table-column label="采购单ID" align="center" prop="purchaseId" />
      <el-table-column label="创建日期" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['erp:put_house:edit']"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            v-hasPermi="['erp:put_house:remove']"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改进仓验货单管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="采购单ID" prop="purchaseId">
          <el-input v-model="form.purchaseId" disabled placeholder="请输入采购单ID" />
        </el-form-item>
        <el-form-item label="是否进库" prop="isPut">
          <el-switch
            v-model="form.isPut"
            :disabled="form.isPut==0?false:true"
            active-color="#13ce66"
            inactive-color="#ddd"
          />
        </el-form-item>
        <el-divider content-position="center">进仓物品信息</el-divider>
        <el-table ref="bPutItem" :data="editHouseList" :row-class-name="rowBPutItemIndex" @selection-change="handleBPutItemSelectionChange">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50" />
          <el-table-column label="物品编号" align="center" prop="itemId" width="150" />
          <el-table-column label="实际进仓数量" prop="putNum" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.putNum" placeholder="请输入实际进仓数量" />
            </template>
          </el-table-column>
          <el-table-column label="仓库名称" prop="houseId" width="200">
            <template slot-scope="scope">
              <el-select v-model="scope.row.houseId" placeholder="仓库名称">
                <el-option
                  v-for="item in houseoptions"
                  :key="item.houseId"
                  :label="item.houseName"
                  :value="item.houseId"
                />
              </el-select>
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPut_house, getPut_house, delPut_house, addPut_house, updatePut_house } from '@/api/erp/puthouse/puthouse'
import { listWarehouse, addWarehouse } from '@/api/erp/warehouse/warehouse'

export default {
  name: 'PutHouse',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedBPutItem: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 进仓验货单管理表格数据
      put_houseList: [],
      // 进仓物品表格数据
      editHouseList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      houseoptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        id: null,
        purchaseId: null,
        createTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询进仓验货单管理列表 */
    getList() {
      this.loading = true
      listPut_house(this.queryParams).then(response => {
        this.put_houseList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        purchaseId: null,
        isPut: null,
        createTime: null,
        isDelete: null
      }
      this.editHouseList = []
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加进仓验货单管理'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getPut_house(id).then(response => {
        listWarehouse().then(res => {
          this.houseoptions = []
          res.rows.forEach(element => {
            this.houseoptions.push({
              houseId: element.id,
              houseName: element.houseName
            })
          })
        })
        console.log(response)
        this.form = response.data
        console.log(this.form)
        if (this.form.isPut) {
          this.form.isPut = true
        } else {
          this.form.isPut = false
        }
        this.editHouseList = response.data.bputItemList
        this.open = true
        this.title = '修改进仓验货单管理'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          this.form.editHouseList = this.editHouseList
          if (this.form.id != null) {
            this.form.isPut = this.form.isPut === true ? 1 : 0
            updatePut_house(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除进仓验货单管理编号为"' + ids + '"的数据项？').then(function() {
        return delPut_house(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {})
    },
    /** 进仓物品序号 */
    rowBPutItemIndex({ row, rowIndex }) {
      row.index = rowIndex + 1
    },
    /** 进仓物品添加按钮操作 */
    handleAddBPutItem() {
      const obj = {}
      obj.itemId = ''
      obj.putNum = ''
      obj.houseId = ''
      this.editHouseList.push(obj)
    },
    /** 进仓物品删除按钮操作 */
    handleDeleteBPutItem() {
      if (this.checkedBPutItem.length === 0) {
        this.$modal.msgError('请先选择要删除的进仓物品数据')
      } else {
        const editHouseList = this.editHouseList
        const checkedBPutItem = this.checkedBPutItem
        this.editHouseList = editHouseList.filter(function(item) {
          return checkedBPutItem.indexOf(item.index) === -1
        })
      }
    },
    /** 复选框选中数据 */
    handleBPutItemSelectionChange(selection) {
      this.checkedBPutItem = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('erp/put_house/export', {
        ...this.queryParams
      }, `put_house_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
